function [err] = to_control(DFILE, NOTEFILE, dtype, dselect)
% update the list/control from the current input file...
% does the basic update, unless type has information...
% if dtype is not empty,
% does an update concatenating ALL PROTOCOLS named "dtype" in the current note list
% if the selection is multiple in the notefile list, concatenates records from all those into one
% list, using the first one as the guide for the basics.
% if dselect is set to 'last', uses only the last protocol,
% or if dselect is set to 'first, uses only the first protocol.

global CONTROL

err = 0;

htag = findobj('Tag', 'NoteFile'); % read the notefile line that has some relevant data in it to parse
seln = get(htag, 'Value');  % note: sel may have multiple entries now

n=size(CONTROL);
sf=n(2)+1;
QueMessage(sprintf('Storing in position %d', sf));
sel = seln(1);
string = cellstr(get(htag, 'String'));
sstr = char(string(sel));

if((nargin > 2) && ~isempty(dtype))
   protocol=unblank(char(dtype)); % use this one.
else
   protocol = unblank(note_args(sstr, 4));  % pick from the line.
%    protocol = NOTEFILE.proto;
end;
CONTROL(sf).seqname = make_sequence(char(DFILE.filename), DFILE.frec);
CONTROL(sf).filename = strcat(char(DFILE.filename), char(DFILE.ext));
CONTROL(sf).path = append_backslash(DFILE.path);
CONTROL(sf).quality = 'good';
CONTROL(sf).mode = DFILE.dmode;
CONTROL(sf).protocol = protocol; % extract the protocol that we ran
CONTROL(sf).solution = '';
CONTROL(sf).pnd = 0;
CONTROL(sf).div = 0;
CONTROL(sf).sequence = note_args(sstr, -5); % extract the sequence from the notefile
if(~isempty(note_args(sstr, 2)))
    CONTROL(sf).recbeg = str2num(note_args(sstr, 2)); % take records from the  notefile display
else
    CONTROL(sf).recbeg = 1;
end;
if(strmatch(DFILE.ext, '.mat'))
   CONTROL(sf).recend = str2num(note_args(sstr, 3));
else
    if(~isempty(note_args(sstr, 3)))
        CONTROL(sf).recend = str2num(note_args(sstr, 3));
    else
    CONTROL(sf).recend = DFILE.records_in_file;
    end;
end;

CONTROL(sf).reclist = sprintf('%d;%d',number_arg(CONTROL(sf).recbeg), number_arg(CONTROL(sf).recend));
CONTROL(sf).skip = 1;
CONTROL(sf).groupavg = 1;
CONTROL(sf).vgain = get_window_arg('Vgain', 10);
CONTROL(sf).igain = get_window_arg('Igain', 1);
CONTROL(sf).iunits = 'nA';
CONTROL(sf).wgain = 1;
CONTROL(sf).tbeg = 0;
CONTROL(sf).tend = 200;
CONTROL(sf).thresh = 0;
CONTROL(sf).jpot = 0;
CONTROL(sf).access = 0;
CONTROL(sf).comp = 0;
CONTROL(sf).alignwin = 0;
CONTROL(sf).leakwin = 2;
if(strcmp(protocol, 'iva') || strcmp(protocol, 'qinactp2')) CONTROL(sf).leakwin = 3; end;
CONTROL(sf).transwin = 2;
CONTROL(sf).transwdur = 45;
CONTROL(sf).deadwin = 0.1;
nstep=length(DFILE.steps);
CONTROL(sf).durho = 0;
CONTROL(sf).durs1 = 0; CONTROL(sf).durs2 = 0; CONTROL(sf).durs3 = 0;
if(DFILE.mode < 5)
   if(nstep > 0)
      CONTROL(sf).durho = number_arg(DFILE.steps(1));
      if(nstep > 1) CONTROL(sf).durs1 = number_arg(DFILE.steps(2)) - number_arg(CONTROL(sf).durho); end
      if(nstep > 2) CONTROL(sf).durs2 = number_arg(DFILE.steps(3)) - number_arg(CONTROL(sf).durs1) - number_arg(CONTROL(sf).durs1); end
      if(nstep > 3) CONTROL(sf).durs3 = number_arg(DFILE.steps(4)) - number_arg(CONTROL(sf).durs2); end
   end
else % for new mode, use stored durations from command file to set our durations correctly (at least first 4)
   if(exist('DFILE.durs'))
      nstep = min(4, length(DFILE.durs));
      CONTROL(sf).durho = DFILE.durs(1);
      if(nstep > 1) CONTROL(sf).durs1 = DFILE.durs(2); end
      if(nstep > 2) CONTROL(sf).durs2 = DFILE.durs(3); end
      if(nstep > 3) CONTROL(sf).durs3 = DFILE.durs(4); end
   else
      nstep = 4;
      CONTROL(sf).durho = 5;
      if(nstep > 1) CONTROL(sf).durs1 = 100; end
      if(nstep > 2) CONTROL(sf).durs2 = 0; end
      if(nstep > 3) CONTROL(sf).durs3 = 0; end
   end;
end;
if(DFILE.mode == 9)
    CONTROL(sf).vgain = 0.1;
end;

CONTROL(sf).diff = 0;
CONTROL(sf).file = '';
CONTROL(sf).altmode = 0;
CONTROL(sf).bactn = 1;
CONTROL(sf).cycle = 1000;
CONTROL(sf).bstart = 0;
CONTROL(sf).bend = 2;

CONTROL(sf).vlmax = -60;
CONTROL(sf).vlmin = -90;
CONTROL(sf).leakt = 11;
CONTROL(sf).Gin = 0;
CONTROL(sf).Rin = 0;
CONTROL(sf).iHold = 0;
CONTROL(sf).Vm0 = 0;
CONTROL(sf).Rmp = 0;
ch = clock;
CONTROL(sf).entrydate = strcat(date, sprintf(' %02.0f:%02.0f:%02.0f', ch(4), ch(5), ch(6)));
CONTROL(sf).lastchanged = strcat(date, sprintf(' %02.0f:%02.0f:%02.0f', ch(4), ch(5), ch(6)));
CONTROL(sf).subtract = 'no';
CONTROL(sf).linkfile = '';
CONTROL(sf).sub_frec = 1;
CONTROL(sf).sub_lrec = 1;
CONTROL(sf).sub_access = 0;
CONTROL(sf).sub_comp = 0;
CONTROL(sf).sub_igain = 1;
if(~check_field(CONTROL(sf), 'CellType'))
   CONTROL(sf).CellType = '?';
end

if(~check_field(CONTROL(sf), 'Commentary'))
   CONTROL(sf).Commentary = ' ';
end

if(DFILE.mode == 3 || DFILE.mode == 9)
    CONTROL(sf).Commentary = DFILE.comment;
end;

if(length(seln) > 1 ) % do the combination here?
   RL_str = [];
   ncomb = 0;
   for sel = seln % build the whole record list.
      sstr = char(string(sel));
      r_str = sprintf('%s;%s', deblank(note_args(sstr,2)), deblank(note_args(sstr,3))); 
      ncomb = ncomb + 1;
      if(strcmp(protocol, 'ltp_base') & ncomb > 2) % special
         r_str = [r_str '/2'];
      end;
      if(~isempty(RL_str))
         RL_str = [RL_str ',' r_str];
      else
         RL_str = r_str;
      end;
   end;
   CONTROL(sf).reclist = RL_str;
   RL=record_parse(CONTROL(sf).reclist);
   CONTROL(sf).recbeg = min(RL); % take records from the  notefile display
   CONTROL(sf).recend = max(RL);
end;

% now modify the list based on what else we may need to do:
switch(nargin)
    case 2
    recs=sprintf('%d;%d',NOTEFILE.frec, NOTEFILE.lrec);
   CONTROL(sf).recbeg=NOTEFILE.frec;
   CONTROL(sf).recend=NOTEFILE.lrec;
   CONTROL(sf).reclist = recs;
     
    
    case 3 % just a dtype - pick all the protocols of the type
   rlist=[]; % we will build a record list of all of the protocol
   l = length(NOTEFILE);
   for i=1:l
      if(strcmp(NOTEFILE(i).proto, dtype))
         recs=sprintf('%d;%d',NOTEFILE(i).frec, NOTEFILE(i).lrec);
         if(~isempty(CONTROL(sf).psp_reclist))
            CONTROL(sf).psp_reclist=strcat(CONTROL(sf).psp_reclist, ',', recs);
         else
            CONTROL(sf).psp_reclist=recs;
         end;
      end;
   end;
   % fill the times with default times.
   CONTROL(sf).stim_time=[10 50];
   CONTROL(sf).psp_time=[9 11 31 31; 9 51 71 71];
   
case 4
   rlist=[]; % we will build a record list of all of the protocol
   l = length(NOTEFILE);
   nfp = strmatch(dtype, {NOTEFILE.proto}) % find protocol matches
   switch(dselect)
   case 'last'
      nfp=nfp(end); % use the last one in the list, not the first one.
   case 'first'
      nfp = nfp(1);
   otherwise
      QueMessage('to_control: bad dselect argument', 1);
      return; % illegal arguments
   end;
   recs=sprintf('%d;%d',NOTEFILE(nfp).frec, NOTEFILE(nfp).lrec);
   CONTROL(sf).recbeg=NOTEFILE(nfp).frec;
   CONTROL(sf).recend=NOTEFILE(nfp).lrec;
   CONTROL(sf).reclist = recs;
   
   
otherwise
end;


if(~isempty(NOTEFILE))
   CONTROL(sf).NOTEFILE=NOTEFILE; % install the current notefile.
end;

% make the data available.....
% setuserdata(CONTROL);

% now update the visible list
[NSF]=setlist;
toggle_display('List');
h1=findobj('Tag', 'CTL');
maxsf = length(CONTROL);
set(h1, 'Value', sf); % select the new argument
if(sf > maxsf) % set an appropriate valute
    sf = 1;
end;
if(sf < 1) sf = 1; end;
if(sf > 15)
   sf = sf - 5;
   set(h1, 'ListboxTop', sf); % scroll the list so new one is in view
else
   set(h1, 'ListboxTop', 1); % unles its just a short list, then just set to top.
end
return;
